package com.gserver;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gserver.commons.script.ScriptManager;
import com.gserver.game.domain.OnlinePlayer;
import com.gserver.game.plugin.impl.PluginManagerImpl;
import com.gserver.netty.ServerPipelineFactory;

/** 
*
*/
public class ServerTest {
	private static final Logger logger = LoggerFactory
			.getLogger(ServerTest.class);

	public static void main(String[] args) {

		ApplicationContext factory = new FileSystemXmlApplicationContext(
				new String[] { "config/applicationContext_init.xml" });

		// ((AbstractApplicationContext) factory).registerShutdownHook();
		
		ServerBootstrap bootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		ServerPipelineFactory httpServerPipelineFactory = (ServerPipelineFactory) factory
				.getBean("serverPipelineFactory");
		bootstrap.setPipelineFactory(httpServerPipelineFactory);
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);
		// 启动端口
		int port = 8888;
		bootstrap.bind(new InetSocketAddress(port));
		logger.info("{} server is starting……", port);

		/**********************以下功能需要优化******************************************************/
		ScriptManager scriptManager = new ScriptManager("utf-8", "javascript");
		String path = "game_server_plugins.cfg.js";
		System.out.println(path);
		PluginManagerImpl pluginManagerImpl = new PluginManagerImpl(path,
				scriptManager);
		pluginManagerImpl.start();
		// 增加JVM停机时的处理
		Runtime.getRuntime().addShutdownHook(new Thread() {

			@Override
			public void run() {
				System.out.println("stoped");
			}
		});
	}

}
